###################################################################################################
# Making Table of Congressional Prohibition Votes #################################################
# Michael Olson and James Snyder ##################################################################
###################################################################################################

# this file does two main tasks: first, it produces a table with the roll call votes that comprise
# our outcome measure for the congressional analysis. second, it produces descriptive statistics
# about ballot roll-off/turnout in these elections relative to 

# read in data on alcohol votes ###################################################################

  alc_votes <- as.data.frame(read_dta("./alcohol_related_votes.dta"))

# limit to chamber

  alc_votes <- alc_votes[alc_votes$chamber=="H",]

# limit to one row per vote
  
  alc_votes <- alc_votes[!duplicated(alc_votes[,c("congress","votenum")]),c("congress","votenum",
                                                                            "year","descr_vv")]
  
  n_votes_total <- nrow(alc_votes[(alc_votes$congress>=62 & alc_votes$congress<=66) | 
                   (alc_votes$congress>=71 & alc_votes$congress<=73),])
  
  print(n_votes_total)
  
  # fileConn<-file("./results/n_votes_total.tex")
  # writeLines(format(round(n_votes_total,2),0), fileConn)
  # close(fileConn)

# load in the votes that we consider
  
  early <- read.csv("./better_prohibition_rollcall_longdat_1910.csv")
    early_votes <- unique(as.character(early$vote_num))
  late <- read.csv("./better_prohibition_rollcall_longdat_1930.csv")
    late_votes  <- unique(as.character(late$vote_num))
    
  all_votes <- as.character(c(early_votes,late_votes))
  
  print(length(all_votes))

  # fileConn<-file("./results/n_votes_used.tex")
  # writeLines(format(round(length(all_votes),2),0), fileConn)
  # close(fileConn)
  
# subset the information to the votes included in our sample
  
  alc_votes$vote_num <- paste(alc_votes$congress,alc_votes$votenum,sep="_v")
  alc_votes <- alc_votes[is.element(alc_votes$vote_num,all_votes),]
  alc_votes$vote_num <- NULL
  
# make the column names pretty and clean up the descriptions  
  
  colnames(alc_votes) <- c("Congress","Voteview Vote Number","Year","Description")
  alc_votes$Description <- unlist(lapply(alc_votes$Description,function(x) paste(str_sub(x,1,1),tolower(str_sub(x,2,nchar(x))),sep="")))
  alc_votes$Year <- as.character(alc_votes$Year); alc_votes$Year <- NULL
  alc_votes$Description <- gsub("&","and",fixed=TRUE,alc_votes$Description)
  
  replacements <- c("Volstead","Hawaii","Alaska","D.C.","Natl. Prohibition Act","18th Amend.","U.S.")
  lowercase    <- tolower(replacements)
  
  for(j in 1:length(replacements)){alc_votes$Description <- gsub(lowercase[j],replacements[j],fixed=T,alc_votes$Description)}
  
  alc_votes$Description <- gsub("Amend.","Amendment",fixed=T,alc_votes$Description)
  alc_votes$Description <- gsub("apps.","appropriations",fixed=T,alc_votes$Description)
  alc_votes$Description <- gsub("Elim.","Eliminate",fixed=T,alc_votes$Description)
  alc_votes$Description <- gsub("w/regard","with regard",fixed=T,alc_votes$Description)
  alc_votes$Description <- gsub("manf.","manufacture",fixed=T,alc_votes$Description)
  alc_votes$Description <- gsub("Const.","Constitutional",fixed=T,alc_votes$Description)
  alc_votes$Description <- gsub("ppl","people",fixed=T,alc_votes$Description)
  alc_votes$Description <- gsub("indians","Native Americans",fixed=T,alc_votes$Description)
  alc_votes$Description <- gsub("resol.","resolution",fixed=T,alc_votes$Description)
  alc_votes$Description <- gsub("Constitutional amend","Constitutional amendment",fixed=T,alc_votes$Description)
  alc_votes$Description <- gsub("anyone manufacture","anyone manufacturing",fixed=T,alc_votes$Description)
  alc_votes$Description <- gsub("liquor manufacture","liquor manufacturing",fixed=T,alc_votes$Description)
  alc_votes$Description <- gsub("equip.","equipment",fixed=T,alc_votes$Description)
  alc_votes$Description <- gsub("warrents","warrants",fixed=T,alc_votes$Description)
  alc_votes$Description <- gsub("Volstead act","Volstead Act",fixed=T,alc_votes$Description)
  alc_votes$Description <- gsub("$7.5m not $7.1m","Appropriations to",fixed=T,alc_votes$Description)
  alc_votes$Description <- gsub("$250,000 invest.","Appropriations to investigate",fixed=T,alc_votes$Description)
  alc_votes$Description <- gsub("amendment.","amendment",fixed=T,alc_votes$Description)
  alc_votes$Description <- gsub("Pres. rept.","Presidential report",fixed=T,alc_votes$Description)
  alc_votes$Description <- gsub("prosec. casual viol. natl.","prosecution of casual violations of national",fixed=T,alc_votes$Description)
  alc_votes$Description <- gsub("not legisl","not legislatures",fixed=T,alc_votes$Description)
  
# make the table  

  tab <- stargazer(alc_votes,
                   rownames = FALSE,
                   summary  = FALSE,
                   title = "Congressional Roll Call Votes in Sample",
                   label = "cong_roll_calls",
                   font.size = "scriptsize")
  
  tab <- gsub("ccc","ccl",fixed=T,tab)
  
  cat(tab, sep = '\n', file = "./results/cong_roll_calls.tex")

# get descriptive statistics on roll off ###################################################################
  
  rolloff <- as.data.frame(read_dta("./prohib_referendum_rolloff.dta"))

  rolloff$Region <- factor(ifelse(is.element(rolloff$state,c("AL","AR","FL","GA","LA","MS","NC","SC","TN","TX","VA")),"South","North"))

  rolloff <- rolloff[(rolloff$year>=1910 & rolloff$year<=1920) | (rolloff$year>=1928 & rolloff$year<=1935),]
  
  rolloff <- rolloff[rolloff$state!="MD",]
  
    # drop the referendums that we drop from the analysis--these are already not in other datasets
  
  rolloff <- rolloff[rolloff$state!="SD" | rolloff$year!=1916 | rolloff$referendum_number==1,]
  rolloff <- rolloff[rolloff$state!="WI" | rolloff$year!=1920,]
  
  means   <- aggregate(r~Region,FUN=mean,data=rolloff)
  
    fileConn<-file("./results/mean_n.tex")
    writeLines(format(round(means[means$Region=="North","r"],2),2), fileConn)
    close(fileConn)
  
    fileConn<-file("./results/mean_s.tex")
    writeLines(format(round(means[means$Region=="South","r"],2),2), fileConn)
    close(fileConn)
    
  sds     <- aggregate(r~Region,FUN=sd,data=rolloff)
    
    fileConn<-file("./results/sd_n.tex")
    writeLines(format(round(sds[sds$Region=="North","r"],2),2), fileConn)
    close(fileConn)
    
    fileConn<-file("./results/sd_s.tex")
    writeLines(format(round(sds[sds$Region=="South","r"],2),2), fileConn)
    close(fileConn)
  
  medians <- aggregate(r~Region,FUN=median,data=rolloff)
  
    fileConn<-file("./results/median_n.tex")
    writeLines(format(round(medians[medians$Region=="North","r"],2),2), fileConn)
    close(fileConn)
    
    fileConn<-file("./results/median_s.tex")
    writeLines(format(round(medians[medians$Region=="South","r"],2),2), fileConn)
    close(fileConn)
  
  
